package com.panda.admin.login.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * author: tcy
 * createDate: 2023/7/10
 * description: 登录信息工具类
 */
public class LoginInfoUtil {

    public LoginInfoUtil() {
    }

    public static JSONObject getJwtPayload() {
        try {
            String payload = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("payload");
            if (StrUtil.isBlank(payload)) {
                return new JSONObject();
            } else {
                return JSONUtil.parseObj(URLDecoder.decode(payload, StandardCharsets.UTF_8.name()));
            }
        } catch (Exception var2) {
            var2.printStackTrace();
            return new JSONObject();
        }
    }

    public static Long getUserId() {
        Long id = getJwtPayload().getLong("user_id");
        return id == null ? 0L : id;
    }

    public static String getUsername() {
        String username = getJwtPayload().getStr("user_name");
        return StrUtil.isBlank(username) ? "用户名不存在" : username;
    }

    public static String getOpenId() {
        String openid = getJwtPayload().getStr("open_id");
        return StrUtil.isBlank(openid) ? "openid不存在" : openid;
    }

    public static String getMobile() {
        String mobile = getJwtPayload().getStr("mobile");
        return StrUtil.isBlank(mobile) ? "mobile不存在" : mobile;
    }

    public static String getChineseName() {
        String name = getJwtPayload().getStr("nick_name");
        return StrUtil.isBlank(name) ? "name不存在" : name;
    }

    public static String getRoleId() {
        String roleId = getJwtPayload().getStr("role_id");
        return StrUtil.isBlank(roleId) ? "roleId不存在" : roleId;
    }

    public static String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        } catch (Exception e) {
            return null;
        }
    }

}
